package com.ja.openchat.ui.chat.ext.core;

import com.ja.openchat.BuildConfig;
import com.zy.openchat.core.model.config.AppConfigInfo;
import com.zy.openchat.core.model.message.ChatInfo;
import com.ja.openchat.ui.chat.ext.AlbumExt;
import com.ja.openchat.ui.chat.ext.CameraExt;
import com.ja.openchat.ui.chat.ext.ContactExt;
import com.ja.openchat.ui.chat.ext.FileExt;
import com.ja.openchat.ui.chat.ext.LocationExt;
import com.ja.openchat.ui.chat.ext.RedPacketExt;
import com.ja.openchat.ui.chat.ext.TransExt;
import com.ja.openchat.ui.chat.ext.VideoCallExt;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;


public class ConversationExtManager {
    private static ConversationExtManager instance;
    private List<ConversationExt> conversationExts;

    private ConversationExtManager() {
        conversationExts = new ArrayList<>();
        init();
    }

    public static synchronized ConversationExtManager getInstance() {
        if (instance == null) {
            instance = new ConversationExtManager();
        }
        return instance;
    }

    private void init() {
        AppConfigInfo configInfo = AppConfigInfo.getAppWithoutLoginConfigInfo();

        registerExt(AlbumExt.class);
        registerExt(CameraExt.class);
        if(BuildConfig.ShowLocal_VoiceChat){
            registerExt(VideoCallExt.class);
        }
        if(configInfo==null||configInfo.can_send_location){
            registerExt(LocationExt.class);
        }
        if(configInfo==null||configInfo.can_send_redpacket){
            registerExt(RedPacketExt.class);
        }
        if(configInfo==null||configInfo.can_remit){
            registerExt(TransExt.class);
        }
        registerExt(ContactExt.class);
        if(configInfo==null||configInfo.can_send_file){
            registerExt(FileExt.class);
        }

    }

    public void registerExt(Class<? extends ConversationExt> clazz) {
        Constructor constructor;
        try {
            constructor = clazz.getConstructor();
            ConversationExt ext = (ConversationExt) constructor.newInstance();
            conversationExts.add(ext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void unregisterExt(Class<? extends ConversationExt> clazz) {

    }

    public List<ConversationExt> getConversationExts(ChatInfo conversation) {
        List<ConversationExt> currentExts = new ArrayList<>();
        for (ConversationExt ext : this.conversationExts) {
            if (!ext.filter(conversation)) {
                currentExts.add(ext);
            }
        }
        return currentExts;
    }
}
